/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatserver;

import java.io.*;
import java.net.*;


/**
 *
 * @author theInterloper
 */
public class ChatServer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ServerSocket server1;
        ServerSocket server2;
        
        Socket connection1;
        ObjectOutputStream output1;
        ObjectInputStream input1;
        String m1 = null;
        
        Socket connection2;
        ObjectOutputStream output2;
        ObjectInputStream input2;
        String m2 = null;
        
        try{
            server1 = new ServerSocket (5000, 100);
            server2 = new ServerSocket (5001, 100);
            
            while (true){
                connection1 = server1.accept();
                output1 = new ObjectOutputStream (connection1.getOutputStream());
                output1.flush();
                input1 = new ObjectInputStream (connection1.getInputStream());
                     
                connection2 = server2.accept();
                output2 = new ObjectOutputStream (connection2.getOutputStream());
                output2.flush();
                input2 = new ObjectInputStream (connection2.getInputStream());
                

                Screen.message ("Server says: got I/O streams from client2\n");
                String message2 = "SERVER>> client2. You have a server connection \n \n";
                output2.writeObject(message2);
                output2.flush();
                
                
                Screen.message ("Server says: got I/O streams from client1\n");
                String message1 = "SERVER>> client1. You have a server connection";
                output1.writeObject(message1);
                output1.flush();
                
                
                do{
                    try{
                        m1 = (String) input1.readObject();
                        Screen.message("Server says: Message sent to server from client1: " + m1 + "\n");
                        output2.writeObject("Message from client 1: " + m1);
                        output2.flush();
                
                        m2 = (String) input2.readObject();
                        Screen.message("Server says: Message sent to server from client2: " + m2 + "\n");
                        output1.writeObject("Message from client 2: " + m2);
                        output1.flush();

                    }catch(ClassNotFoundException cnf){
                        Screen.message(cnf.getMessage());
                    }
                }while(!m1.equals("Client >> End") || !m2.equals("Client >> End") );
                
                output1.close();
                input1.close();
                connection1.close();
                
                output2.close();
                input2.close();
                connection2.close();

                System.out.println("GOGOGOGO");
                        
            }
    
        }catch (EOFException eof){
            Screen.message("Client termination exception");
        
        }catch (IOException e){
            Screen.message(e.getMessage());
        }
        
        
    }
}

